/**
 * 
 */
package com.java.test.messagecenter.controller;

import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.test.controller.BaseController;
import com.java.test.messagecenter.dto.AppUpdateInfo;
import com.java.test.messagecenter.dto.TerminalInfo;
import com.java.test.messagecenter.service.MessageCenterService;
import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.util.HttpErrorCode;
import com.java.test.util.JsonResult;
import com.java.test.util.PageInfo;

/**
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/terminal")
public class TerminalChannelController extends BaseController {

	@Autowired
	MessageCenterService messageCenterService;

	/**
	 * 终端列表
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/terminalList")
	public JsonResult terminalList(@RequestBody PageInfo<TerminalInfo> info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			PageInfo<TerminalInfo> list = messageCenterService.selectterminalPage(info);
			if (list == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 终端名称查询渠道列表
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/selectchannelListbyterminalname")
	public JsonResult selectchannelListbyterminalname(@RequestBody TerminalInfo info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			List<TerminalInfo> list = messageCenterService.selectallchannelByterminalnName(info.getTerminalName());
			if (list == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 
	 * 更新终端
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateterminalInfo")
	public JsonResult updateterminalInfo(@RequestBody TerminalInfo info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = messageCenterService.updateterminalInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/deleteminalInfo")
	public JsonResult deleteminalInfo(@RequestBody TerminalInfo info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = messageCenterService.deleteminalInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 
	 * 添加终端
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/addterminalInfo")
	public JsonResult addterminalInfo(@RequestBody TerminalInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		// log
		LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

		try {
			// 查看终端是否存在
			TerminalInfo channelIdInfo = messageCenterService.selectterminalbychannelId(info.getTchannelId());
			if (channelIdInfo != null) {
				js.setCode(HttpErrorCode.TERMINALISEXITS.getCode());
				js.setMessage(HttpErrorCode.TERMINALISEXITS.getContent());
				return js;
			}

			info.setAddPeople(user.getLoginAccout());
			info.setAddTime(LocalDateTime.now());
			int i = messageCenterService.insertterminalInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "终端管理-添加终端");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 查询单个终端
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/selectterminalbyId")
	public JsonResult terminalList(@RequestBody TerminalInfo info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			TerminalInfo obj = messageCenterService.selectterminalbyId(info);
			if (obj == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(obj);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/*** ===============app更新================== ***/

	/**
	 * 
	 * app分页查询
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/appupdateList")
	public JsonResult appupdateList(@RequestBody PageInfo<AppUpdateInfo> info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			PageInfo<AppUpdateInfo> list = messageCenterService.selectappupdatePage(info);
			if (list == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 
	 * 查询单个app
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/selectappupdateInfo")
	public JsonResult selectappupdateInfo(@RequestBody AppUpdateInfo info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			AppUpdateInfo infos = messageCenterService.selectappupdateInfo(info);
			if (infos == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(infos);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 
	 * 添加app更新
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/addappupdateInfo")
	public JsonResult addterminalInfo(@RequestBody AppUpdateInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			info.setAddPeople(user.getLoginAccout());
			info.setCreateTime(LocalDateTime.now());
			int i = messageCenterService.insertappupdateInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "终端管理-添加APP更新");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 
	 * 添加app更新
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/deleteappupdateInfo")
	public JsonResult deleteappupdateInfo(@RequestBody AppUpdateInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
			info.setAddPeople(user.getLoginAccout());
			info.setCreateTime(LocalDateTime.now());
			int i = messageCenterService.deleteappupdateInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "终端管理-删除APP更新");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 
	 * 修改app更新
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateappupdateInfo")
	public JsonResult updateappupdateInfo(@RequestBody AppUpdateInfo info) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = messageCenterService.updateappupdateInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

}
